﻿package src.components {
	
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import fl.controls.TextInput;
	import fl.controls.TextArea;
	import flash.utils.Dictionary;
	import fl.controls.ComboBox;
	//src
	import src.util.Utils;
	import src.computation.Recovery;
	import src.components.ElForm;
	// astra
	import com.yahoo.astra.containers.formClasses.FormItem;
	import com.yahoo.astra.containers.formClasses.FormLayoutStyle;
	import com.yahoo.astra.layout.LayoutContainer;	
	import com.yahoo.astra.layout.modes.BoxLayout;
	import com.yahoo.astra.fl.controls.TabBar;
	
	public class ElementForm extends ElForm {
		
		private var elementTypeComboBox;
		
		public function ElementForm(textHeader:String = "") {
			super(textHeader);	
		}
		
		protected override function initUI() {
			elementTypeComboBox = new ComboBox();
			elementTypeComboBox.width = 150;				
			
			nameInput = new TextInput();
			nameInput.width = 150;	
			
			description = new TextArea();			
			description.width = 150;
			description.height = 80;
			
			this.createDistrLawPanel();
			this.recoveryPanel = new Recovery();
			var box : BoxLayout = new BoxLayout();
			box.direction = "horizontal";
			box.horizontalGap = 5;			
			this.recoveryContainer = new LayoutContainer(box);	
			this.recoveryContainer.addChild(this.recoveryPanel);
			
			this.tabs = new TabsPanel([ { label:"Закон распределения", panel:this.distrLawPanel },
										{label:"Востановление",panel:this.recoveryContainer}]);		
			// button
			this.createOkAndCancel();
		}
		
		protected override function initData() {		
			var typeFormItem = new FormItem("Тип элемента", this.elementTypeComboBox);
			typeFormItem.labelAlign = FormLayoutStyle.TOP;
			typeFormItem.labelWidth = 80;
			
			nameFormItem = new FormItem("Имя", nameInput);					
			nameFormItem.labelAlign = FormLayoutStyle.TOP;
			nameFormItem.labelWidth = 30;			
			
			var box0 : BoxLayout = new BoxLayout();
			box0.direction = "vertical";
			box0.horizontalGap = 10;			
			var nameNumContainer = new LayoutContainer(box0);
			nameNumContainer.autoMask = false;
			nameNumContainer.addChild(nameFormItem);
			nameNumContainer.addChild(typeFormItem);
			//
			descriptionFormItem = new FormItem("Описание", description);
			descriptionFormItem.labelAlign = FormLayoutStyle.TOP;
			descriptionFormItem.labelWidth = 50;					
			
			var box : BoxLayout = new BoxLayout();
			box.direction = "horizontal";
			box.horizontalGap = 5;			
			var nameDescContainer = new LayoutContainer(box);	
			nameDescContainer.autoMask = false;
			nameDescContainer.addChild(nameNumContainer);
			nameDescContainer.addChild(descriptionFormItem);			
			//			
			if(this.distrlaw!=null){
				this.renderDisrtrLowForm(this.distrlaw);
			}			
			
			var buttons = new FormItem("", [this.okButton, this.cancelButton]);
			buttons.labelWidth = 80;	
			
			var generalBox : BoxLayout = new BoxLayout();
			generalBox.direction = "vertical";
			generalBox.verticalGap = 10;			
			this.itemContainer = new LayoutContainer(generalBox);	
			itemContainer.autoMask = false;
			itemContainer.x = itemContainer.y = 30;
			itemContainer.addChild(nameDescContainer);			
			itemContainer.addChild(this.tabs);
			itemContainer.addChild(buttons);			
			this.form.addChild(itemContainer);
		}
		
		public override function getData():Dictionary {
			var dict:Dictionary = new Dictionary();
			dict["name"] = nameInput.text;
			dict["description"] = description.text;			
			dict["law"] = this.distrlaw;
			dict["recovery"] = this.recoveryPanel;
			var obj = this.elementTypeComboBox.selectedItem;
			if (obj != null){				
				dict["stockId"] = obj.stockId;
			}else {
				dict["stockId"] = null;
			}
			return dict;
		}
		
		public override function setData(dict: Dictionary) {			
			if (dict["stock"] && dict["stock"]) {
				var dpStock = dict["stock"];
				this.elementTypeComboBox.dataProvider.addItem({label:"",stockId:-1});
				for (var i = 0; i < dpStock.length; i++) {					
					var obj = dpStock.getItemAt(i);				
					this.elementTypeComboBox.dataProvider.addItem({label:obj.type,stockId:obj.stockId});
				}				
				
			}			
			if (dict["stockId"]) {
				var index = null;
				for (var i = 1; i < this.elementTypeComboBox.dataProvider.length; i++) {	
					var obj =  this.elementTypeComboBox.dataProvider.getItemAt(i);
					if (obj.stockId == dict["stockId"]) {
						index = i;
						break;
					}
				}
				if (index != null) {
					this.elementTypeComboBox.selectedIndex = index;
				}
			}
			if(dict["name"] && dict["name"]!=null)
				nameInput.text = dict["name"];
			if(dict["description"] && dict["description"]!=null)
				description.text = dict["description"];					
			if(dict["law"] && dict["law"]!=null){				
				this.distrlaw = dict["law"];
				this.renderDisrtrLowForm(this.distrlaw);
			}
			if (dict["recovery"] && dict["recovery"] != null) {
				this.renderRecoveryPanel(dict["recovery"]);
			}
		}
		
	}
}